#!/usr/bin/php
<?php
/*
 * Copyright © 2020 Anonymous
 * This program is free software. It comes without any warranty, to the extent permitted by applicable law. You can redistribute it and/or modify it under the
 * terms of the Do What The Fuck You Want To Public License, Version 2,
 * as published by Sam Hocevar. See the COPYING file for more details.
 */

if ($argc < 3 || $argc > 4)
{
	fwrite(STDERR, "Usage: keygen.php [domains] [expires] [comment]" . PHP_EOL);
	fwrite(STDERR, "domains is comma-separated list of domains" . PHP_EOL);
	fwrite(STDERR, "expires is license expiration date" . PHP_EOL);
	fwrite(STDERR, "comment is optional license comment" . PHP_EOL);
	fwrite(STDERR, "Expiration date must be expressed according to ISO 8601." . PHP_EOL);
	fwrite(STDERR, "Asterisk in place of expiration date should be used for permanent license." . PHP_EOL);
	fwrite(STDERR, "Using non-date values is not recommended." . PHP_EOL);
	exit();
}

$domains = $argv[1];
$expires = $argv[2];

if (empty($domains) || empty($expires))
{
	fwrite(STDERR, "Invalid license." . PHP_EOL);
	exit();
}

if (isset ($argv[3]))
{
	$comment = $argv[3];
}
else
{
	$comment = '';
}


$license = $domains.'#'.$expires.'#'.$comment;


$p = 11;
$g = 2;
$x = 7; //rand(2, $p-1);
$y = pow($g, $x) % $p; // same as x

$message = bin2hex($license);
$message = str_split($message, 4);

$key = '';
$shift = $x;

foreach($message as $block)
{	
	$block = base_convert($block, 16, 10);
	$enc_block = '';
	for($i = 0; $i < strlen($block); $i++)
	{
		$k = rand(2, $p - 1);
		$a = pow($g, $k) % $p + ($i + $shift) % 26;
		$b = ( (pow($y % $p, $k) % $p) * ($block[$i]) ) % $p + ($i + $shift) % 25;
		
		$enc_block .= base_convert($a, 10, 36).base_convert($b, 10, 36);
	}
	$key .= $enc_block.' ';
	$shift += $x;
}

print $key.PHP_EOL;
